<?php

class CultivateService extends AbstractService
{

    /**
     * @var CultivateService
     */
    private static $instance;

    /**
     * Get the instance of CultivateService.
     *
     * @return CultivateService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new CultivateService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    protected function __construct()
    {
        parent::__construct();
    }

    /**
     * 处理用户创建时的逻辑
     *
     * @param int $shardId
     * @return void
     */
    public function handleCreate($shardId)
    {

    }

    /**
     * 处理用户登录时的逻辑
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId)
    {
        // do nothing
    }

    /**
     * 处理 service 对外提供的全量数据
     *
     * @param int $userId
     * @return array
     */
    public function info($userId)
    {
        $userLandList = $this->initHelpWaterNum($userId);
        $userFlowerList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_FLOWER);
        return array($userLandList, $userFlowerList);
    }

    /**
     * 解锁地块
     * @param $userId
     * @param $landId
     * @return bool|void
     * @throws SzException
     */
    public function unlockLand($userId, $landId)
    {
        $landInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_LAND_SET, $landId, true);
        if (!$landInfo) {
            throw new SzException(29001, $landId);
        }

        $profile = SzPersister::get()->getVo($userId, ModuleProfileConst::ORM_NAME_PROFILE);
        if ($profile->getLevel() < $landInfo['unlockLv']) {
            throw new SzException(29002);
        }

        $userLandList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_LAND);
        $userLand = $userLandList->getElement($landId, false);
        if ($userLand) {
            throw new SzException(29003, $landId);
        }

        if (!$landInfo['unlockItem']) {
            throw new SzException(29004, $landId);
        }

        // 扣除道具
        $tid = '';
        foreach ($landInfo['unlockItem'] as $entityId => $count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true);
            $tid .= $entityId . ':' . abs($count) . '|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(99999984,$userId,0,$tid,'unlockLand');//消耗资源上报

        // 新增土地记录
        $cultivateLand = new CultivateLandVo($userId, $landId, 0, 0, GameConst::DEFAULT_JSON_STRING, 0, true);
        $userLandList->addElement($cultivateLand);
        $userLandList->setElement($landId, $cultivateLand);
        SzPersister::get()->setVoList($userLandList);

        $action = 'discolour';
        if ($landInfo['landType'] == 2) {
            $action = 'cultivate';
        }
        // 数据采集:解锁地块
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:' . $action . '|action:unlock|',
                'item' => $landId,
                'value' => 1
            )
        ));

        // 数据采集:解锁地块消耗
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => $tid,
                'item' => $action . '_unlock&' . $landId
            )
        ));

        // 数据采集：田地块扩张
        $count = count($userLandList->getList());
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_UPGRADE, array(
            'clientData' => array(
                'tid' => 'objective:' . $action . '|item:unlock|',
                'item' => 'fromlevel:' . ($count - 1) . '|changelev:1|targetlev:' . $count . '|',
                'value' => 1
            )
        ));
        return true;
    }

    /**
     * 培养/变色
     * @param $userId
     * @param $landId
     * @param $flowerId
     * @param $guess
     * @return array
     * @throws SzException
     */
    public function plant($userId, $landId, $flowerId, $guess)
    {
        /**
         * @var CultivateLandVoList $userLandList
         * @var CultivateLandVo $userLand
         * @var CultivateFlowerVoList $userFlowerList
         * @var CultivateFlowerVo $userFlower
         */

        $flowerInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_SEED_CONDITION, $flowerId, true);
        $profile = SzPersister::get()->getVo($userId, ModuleProfileConst::ORM_NAME_PROFILE);
        if ($profile->getLevel() < $flowerInfo['unlockLv']) {
            throw new SzException(29002);
        }

        $landInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_LAND_SET, $landId, true);
        if ($landInfo['landType'] != $flowerInfo['unlockType']) {
            throw new SzException(29006);
        }

        foreach ($guess as $count) {
            if ($count < 1) {
                throw new SzException(29005);
            }
        }

        $userLandList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_LAND);
        $userLand = $userLandList->getElement($landId);

        if ($userLand->getFlowerId()) {
            throw new SzException(29007, $landId);
        }

        $seedList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_PLANT_SEED);
        $seedVo = $seedList->getElement($flowerId, false);
        if ($seedVo) {
            throw new SzException(29008, $flowerId);
        }

        $userFlowerList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_FLOWER);
        $userFlower = $userFlowerList->getElement($flowerId, false);

        // 扣除道具
        $inputTid = '';
        foreach ($flowerInfo['itemId'] as $index => $info) {
            $k = 0;
            foreach ($info as $entityId => $count) {
                GameService::get()->handleEntity($userId, $entityId, $guess[$k], true);
                $k++;
                $inputTid .= $entityId . ':' . abs($count) . '|';
            }
            break;
        }

        $action = 'discolour';
        if ($flowerInfo['unlockType'] == 2) {
            list($itemList) = ItemService::get()->processItems($userId, [$flowerId => -1]);
            SzPersister::get()->setVoList($itemList);
            $inputTid .= '7401' . $flowerId . ':1|';
            $action = 'cultivate';
        }

        if (!$userFlower) {
            $userFlower = new CultivateFlowerVo($userId, $flowerId, GameConst::DEFAULT_JSON_STRING, GameConst::DEFAULT_JSON_STRING, 0, true);
        }

        $guessRes = false;
        $weightKey = 0;
        // 是否一致
        foreach ($flowerInfo['itemId'] as $index => $info) {
            $same = true;
            $k = 0;
            foreach ($info as $count) {
                if ($count != $guess[$k]) {
                    $same = false;
                    break;
                }
                $k++;
            }
            if ($same) {
                $guessRes = true;
                $weightKey = $index;
                break;
            }
        }

        if ($guessRes) {
            $weight = $flowerInfo['probability'][$weightKey];
            $rand = mt_rand(1, 100);
            if ($weight < $rand) {
                $guessRes = false;
            }
        }

        if ($guessRes) {
            // 解锁成功
            $userLand->setUnlock_res(1);


        } else {
            $res = array();
            $total = 0;
            $flower = array();
            foreach ($flowerInfo['dropSeed'][0] as $entityId => $weight) {
                list(, , $subId) = EntityService::get()->parseEntityId($entityId);
                $subId -= 40000;
                $seedVo = $seedList->getElement($subId, false);
                if (!$seedVo) {
                    continue;
                }
                $total += $weight;
                $flower[$entityId] = $total;
            }
            if (!$flower) {
                $res = $flowerInfo['dropItem'];
            } else if (count($flower) == 1) {
                foreach ($flower as $entityId => $weight) {
                    $res[$entityId] = $flowerInfo['seedNumber'];
                    break;
                }
            } else {
                for ($i = 0; $i < $flowerInfo['seedNumber']; $i++) {
                    $rand = mt_rand(1, $total);
                    foreach ($flower as $entityId => $weight) {
                        if ($rand <= $weight) {
                            if (isset($res[$entityId])) {
                                $res[$entityId] += 1;
                            } else {
                                $res[$entityId] = 1;
                            }
                            break;
                        }
                    }
                }
            }
            // 解锁失败
            $userLand->setFailed_res($res);
        }

        $userLand->setCurrent_info($guess);
        $userLand->setFlowerId($flowerId);
        $userLandList->updateElement($landId, $userLand);
        SzPersister::get()->setVoList($userLandList);

        if ($userFlower->isInsert() && !SzUtility::checkArrayKey($flowerId, $userFlowerList->getList())) {
            $userFlowerList->addElement($userFlower);
            $userFlowerList->setElement($flowerId, $userFlower);
        } else {
            $userFlowerList->updateElement($flowerId, $userFlower);
        }

        SzPersister::get()->setVoList($userFlowerList);

        // 数据采集:鲜花培育/变色
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:flower|action:' . $action . '|',
                'item' => $flowerId,
                'value' => 1
            )
        ));

        CommonFunctionService::get()->reportResourceUseDetails(99999983,$userId,0,$inputTid,'plant');//消耗资源上报
        // 数据采集:鲜花培育/变色消耗
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => $inputTid,
                'item' => 'flower_' . $action . '&' . $flowerId
            )
        ));
        return array($userLand->toArray(), $userFlower->toArray());
    }

    /**
     * 浇水
     * @param $userId
     * @param $landId
     * @return bool|void
     * @throws SzException
     */
    public function water($userId, $landId)
    {
        /**
         * @var CultivateLandVoList $userLandList
         * @var CultivateLandVo $userLand
         */
        $userLandList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_LAND);
        $userLand = $userLandList->getElement($landId);

        $flowerId = $userLand->getFlowerId();
        if (!$flowerId) {
            throw new SzException(29009, $landId);
        }

        $flowerInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_SEED_CONDITION, $flowerId, true);

        $water_num = $userLand->getWater_num();
        $need_water_num = 0;
        foreach ($flowerInfo['consumption'] as $num) {
            $need_water_num = $num;
            break;
        }

        if ($water_num >= $need_water_num) {
            throw new SzException(29010, $landId);
        }

        $tid = '';
        foreach ($flowerInfo['consumption'] as $entityId => $count) {
            GameService::get()->handleEntity($userId, $entityId, 1, true);
            $tid .= $entityId . ':1|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(99999983,$userId,0,$tid,'water');//消耗资源上报

        $userLand->updateWater_num(1);
        $userLandList->updateElement($landId, $userLand);
        SzPersister::get()->setVoList($userLandList);
        $userLandList = $this->initHelpWaterNum($userId);

        $action = 'discolour';
        if ($flowerInfo['unlockType'] == 2) {
            $action = 'cultivate';
        }

        // 数据采集:鲜花培育/变色浇水
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:' . $action . '|action:watering|',
                'item' => $flowerId,
                'value' => 1
            )
        ));

        // 数据采集:鲜花培育/变色浇水消耗
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => $tid,
                'item' => $action . '_watering&' . $flowerId
            )
        ));
        return array($userLandList->toArray());
    }

    /**
     * 帮助培育浇水
     * @param $userId
     * @param $friendId
     * @param $landId
     * @return true
     * @throws SzException
     */
    public function friendWater($userId, $friendId, $landId)
    {
        $totalCount = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'umberOfMutualaid', true);
        //获取当天交互次数
        $friendCount = FriendService::get()->getFriendHandleInfo($userId, $friendId,$totalCount);
        if ($friendCount > $totalCount['value']) {
            throw new SzException(29012);
        }

        if ($userId == $friendId) {
            throw new SzException(29002);
        }
        /**
         * @var CultivateLandVoList $userLandList
         * @var CultivateLandVo $userLand
         */
        $landInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_LAND_SET, $landId, true);
        if (!$landInfo) {
            throw new SzException(29001, $landId);
        }

        $userLandList = SzPersister::get()->getVoList($friendId, GameConst::ORM_NAME_CULTIVATE_LAND);
        $userLand = $userLandList->getElement($landId);

        $flowerId = $userLand->getFlowerId();
        if (!$flowerId) {
            throw new SzException(29009, $landId);
        }

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $redisDb->hIncrBy(GameConst::CACHE_KEY_CULTIVATE_WATER_NUM . $friendId, $landId, 1);

        //修改当天交互次数
        //FriendService::get()->updateFriendHandleInfo($userId, $friendId);
        // 更新任务进度---帮助好友浇水
        MissionService::get()->progress($userId, GameConst::ENTITY_ACTION_HELP_WATER, GameConst::ENTITY_TYPE_NULL, GameConst::ENTITY_PADDING_NULL, 1);

        // 数据采集:帮助好友浇水
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:friends|action:watering|',
                'item' => $friendId . '&' . $flowerId,
                'value' => 1
            )
        ));

        $flowerInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_SEED_CONDITION, $flowerId, true);
        $type = $flowerInfo['unlockType'] == 2 ? 1 : 2;
        // 数据采集:帮助好友浇水消耗交互次数
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => '7415999999999:1|',
                'item' => 'friends_watering&&' . $friendId . '&' . $type
            )
        ));
        return true;
    }

    /**
     * 收获
     * @param $userId
     * @param $landId
     * @return bool|void
     * @throws SzException
     */
    public function harvest($userId, $landId)
    {
        /**
         * @var CultivateLandVoList $userLandList
         * @var CultivateLandVo $userLand
         * @var CultivateFlowerVoList $userFlowerList
         * @var CultivateFlowerVo $userFlower
         */
        $userLandList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_LAND);
        $userLand = $userLandList->getElement($landId);

        $flowerId = $userLand->getFlowerId();
        $userFlowerList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_FLOWER);
        $userFlower = $userFlowerList->getElement($flowerId);
        $currentInfo = $userLand->getRawCurrent_info();

        $flowerId = $userLand->getFlowerId();
        $flowerInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_SEED_CONDITION, $flowerId, true);

        $water_num = $userLand->getWater_num();
        $need_water_num = 0;
        foreach ($flowerInfo['consumption'] as $num) {
            $need_water_num = $num;
            break;
        }

        if ($water_num < $need_water_num) {
            throw new SzException(29010, $landId);
        }
        $cultivated = $userFlower->getCultivated();
        $tid = '';
        if ($userLand->getUnlock_res()) {
            $userFlower->setCultivated(1);
            $seedList = SeedService::get()->unlockSeed($userId, $flowerId);
            // 更新任务进度--培育成功
            if ($flowerInfo['unlockType'] == 2) {
                MissionService::get()->progress($userId, GameConst::ENTITY_ACTION_CULTIVATE, GameConst::ENTITY_TYPE_ITEM, $flowerId, 1);
            } else if ($flowerInfo['unlockType'] == 3) {
                MissionService::get()->progress($userId, GameConst::ENTITY_ACTION_DISCOLORATION, GameConst::ENTITY_TYPE_ITEM, $flowerId, 1);
            }
            $result = 'success';
        } else {
            // 发放奖励
            foreach ($userLand->getRawFailed_res() as $entityId => $count) {
                GameService::get()->handleEntity($userId, $entityId, $count, true);
                $tid .= $entityId . ':' . abs($count) . '|';
            }
            $seedList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_PLANT_SEED);
            $result = 'fail';
        }
        if (!$cultivated) {
            $userFlower->setLast_info($currentInfo);
        }

        $this->initUserLand($userLandList, $userLand, $landId);

        $userFlowerList->updateElement($flowerId, $userFlower);
        SzPersister::get()->setVoList($userFlowerList);

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $redisDb->hDel(GameConst::CACHE_KEY_CULTIVATE_WATER_NUM . $userId, $landId);


        $action = 'discolour';
        if ($flowerInfo['unlockType'] == 2) {
            $action = 'cultivate';
        }

        // 数据采集:鲜花培育/变色结果
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:' . $action . '|action:result|',
                'item' => $flowerId . '&' . $result,
                'value' => 1
            )
        ));

        if ($tid) {
            CommonFunctionService::get()->reportResourceUseDetails(99999983,$userId,1,$tid,'harvest');//获得资源上报
            // 数据采集:鲜花培育/变色结果产出
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_OUTPUT, array(
                'clientData' => array(
                    'tid' => $tid,
                    'item' => $action . '_result&' . $flowerId . '&' . $result
                )
            ));
        }
        return array($seedList->toArray());
    }

    /**
     * 铲除
     * @param $userId
     * @param $landId
     * @throws SzException
     */
    public function move($userId, $landId)
    {
        /**
         * @var CultivateLandVoList $userLandList
         * @var CultivateLandVo $userLand
         * @var CultivateFlowerVoList $userFlowerList
         * @var CultivateFlowerVo $userFlower
         */
        $userLandList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_LAND);
        $userLand = $userLandList->getElement($landId);
        $flowerId = $userLand->getFlowerId();
        $this->initUserLand($userLandList, $userLand, $landId);
        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $redisDb->hDel(GameConst::CACHE_KEY_CULTIVATE_WATER_NUM . $userId, $landId);


        $flowerInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_SEED_CONDITION, $flowerId, true);
        $action = 'discolour';
        if ($flowerInfo['unlockType'] == 2) {
            $action = 'cultivate';
        }

        // 数据采集:鲜花培育/变色铲除鲜花
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:' . $action . '|action:cut|',
                'item' => $flowerId,
                'value' => 1
            )
        ));
        return true;
    }

    /**
     * 帮助信息列表
     * @param $userId
     * @param $friendId
     * @return array
     * @throws SzException
     */
    public function getHelpInfo($userId, $friendId)
    {
        $landInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_LAND_SET, null, true);
        if (!$landInfo) {
            throw new SzException(29005);
        }
        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $helpList = array();
        foreach ($landInfo as $val) {
            $landId = $val['landID'];
            $data = $redisDb->hGet(GameConst::CACHE_KEY_CULTIVATE_HELP . $userId . ':' . $landId, $friendId);
            // 被帮助的人数
            $helpList[$landId] = $data ? $data : 0;
        }

        $helpNum = $redisDb->hGet(GameConst::CACHE_KEY_CULTIVATE_HELPED, $friendId);
        $helpNum = $helpNum ? $helpNum : 0;
        return array($helpList, $helpNum);
    }

    /**
     * @param $userId
     * @return CultivateLandVoList|SzAbstractVoList|void
     * @throws SzException
     */
    public function initHelpWaterNum($userId)
    {
        /**
         * @var CultivateLandVoList $userLandList
         * @var CultivateLandVo $userLand
         */
        $landInfos = SzConfig::get()->loadAppConfig(GameConst::CONFIG_LAND_SET, null, true);
        $userLandList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_LAND);
        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        foreach ($landInfos as $landInfo) {
            $landId = $landInfo['landID'];
            $userLand = $userLandList->getElement($landId, false);
            if (!$userLand) {
                continue;
            }
            $flowerId = $userLand->getFlowerId();
            if (!$flowerId) {
                continue;
            }
            $helpWaterNum = $redisDb->hGet(GameConst::CACHE_KEY_CULTIVATE_WATER_NUM . $userId, $landId);
            $waterNum = $userLand->getWater_num();
            $flowerInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_SEED_CONDITION, $flowerId, true);
            $need_water_num = 0;
            foreach ($flowerInfo['consumption'] as $num) {
                $need_water_num = $num;
                break;
            }
            if ($waterNum < $need_water_num) {
                // 帮助的不够
                if (($waterNum + $helpWaterNum) <= $need_water_num) {
                    $userLand->updateWater_num($helpWaterNum);
                    $redisDb->hIncrBy(GameConst::CACHE_KEY_CULTIVATE_WATER_NUM . $userId, $landId, -$helpWaterNum);
                }
                // 帮助的超过
                if (($waterNum + $helpWaterNum) > $need_water_num) {
                    $userLand->updateWater_num($need_water_num - $waterNum);
                    $redisDb->hIncrBy(GameConst::CACHE_KEY_CULTIVATE_WATER_NUM . $userId, $landId, -($need_water_num - $waterNum));
                }
                $userLandList->updateElement($landId, $userLand);
            }
        }
        SzPersister::get()->setVoList($userLandList);
        return $userLandList;
    }

    /**
     * 解锁免费
     * @param $userId
     * @throws SzException
     */
    public function flushUnlockLand($userId)
    {
        $landInfos = SzConfig::get()->loadAppConfig(GameConst::CONFIG_LAND_SET, null, true);
        if (!$landInfos) {
            return;
        }
        $profile = SzPersister::get()->getVo($userId, ModuleProfileConst::ORM_NAME_PROFILE);
        $userLandList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_LAND);
        foreach ($landInfos as $landInfo) {
            $landId = $landInfo['landID'];
            if ($profile->getLevel() < $landInfo['unlockLv']) {
                continue;
            }
            $userLand = $userLandList->getElement($landId, false);
            if ($userLand) {
                continue;
            }
            if (!$userLand && !$landInfo['unlockItem']) {
                // 新增土地记录
                $cultivateLand = new CultivateLandVo($userId, $landId, 0, 0, GameConst::DEFAULT_JSON_STRING, 0, true);
                $userLandList->addElement($cultivateLand);
                $userLandList->setElement($landId, $cultivateLand);
            }
        }
        SzPersister::get()->setVoList($userLandList);
    }

    private function initUserLand($userLandList, $userLand, $landId)
    {
        /**
         * @var CultivateLandVoList $userLandList
         * @var CultivateLandVo $userLand
         */
        $userLand->setWater_num(0);
        $userLand->setFailed_res(array());
        $userLand->setFlowerId(0);
        $userLand->setUnlock_res(0);
        $userLand->setCurrent_info(array());
        $userLandList->updateElement($landId, $userLand);
        SzPersister::get()->setVoList($userLandList);
    }

    public function getNeedWater($userId)
    {
        $landInfos = SzConfig::get()->loadAppConfig(GameConst::CONFIG_LAND_SET, null, true);
        if (!$landInfos) {
            return false;
        }

        /**
         * @var CultivateLandVoList $userLandList
         * @var CultivateLandVo $userLand
         */
        $userLandList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CULTIVATE_LAND);
        foreach ($landInfos as $landInfo) {
            $landId = $landInfo['landID'];
            $userLand = $userLandList->getElement($landId, false);
            if (!$userLand) {
                continue;
            }
            $waterNum = $userLand->getWater_num();
            $flowerInfo = SzConfig::get()->loadAppConfig(GameConst::CONFIG_SEED_CONDITION, $userLand->getFlowerId(), true);
            $need_water_num = 0;
            foreach ($flowerInfo['consumption'] as $num) {
                $need_water_num = $num;
                break;
            }
            if ($need_water_num > $waterNum) {
                return true;
            }
        }
        return false;
    }

}